package com.service;

import static com.def.GenericDef.BANK_IN;
import static com.def.GenericDef.BANK_OUT;
import static com.def.GenericDef.EVT_CANCEL_UNPACK;
import static com.def.GenericDef.EVT_RMA_CANCEL_PRD_IN;
import static com.def.GenericDef.EVT_RMA_CANCEL_RELEASE;
import static com.def.GenericDef.EVT_RMA_PRD_IN;
import static com.def.GenericDef.EVT_RMA_RELEASE;
import static com.def.GenericDef.EVT_RMA_STB_INPR;
import static com.def.GenericDef.EVT_STB_INPR;
import static com.def.GenericDef.E_BIS_MDL_DEF;
import static com.def.GenericDef.E_BIS_PATH;
import static com.def.GenericDef.E_BIS_PATHI;
import static com.def.GenericDef.E_BIS_TOOL_GR;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_BOX;
import static com.def.GenericDef.E_RET_PRD_IN;
import static com.def.GenericDef.E_RET_PRD_INFO;
import static com.def.GenericDef.E_RET_PRD_INFO_SHTC;
import static com.def.GenericDef.E_RET_WO;
import static com.def.GenericDef.E_XPMOVEOUT_WO_UNMATCH;
import static com.def.GenericDef.E_XPRMA_INVALID_STATUS;
import static com.def.GenericDef.E_XPRMA_INVALID_WO;
import static com.def.GenericDef.E_XPRMA_INVALID_WO_STATUS;
import static com.def.GenericDef.E_XPRMA_MDL_ID_NULL;
import static com.def.GenericDef.E_XPRMA_NO_SO;
import static com.def.GenericDef.E_XPRMA_OVER_STD_CNT;
import static com.def.GenericDef.E_XPRMA_SLOT_NO_EXIST;
import static com.def.GenericDef.E_XPRMA_MTRL_BOX_INVALIED;
import static com.def.GenericDef.E_XPRMA_PRD_IN_NOT_ENOUGH;
import static com.def.GenericDef.E_XPUNPACK_MTRL_BOX_INVALIED;
import static com.def.GenericDef.E_XPWMSOPE_INVALID_INPUT;
import static com.def.GenericDef.LOT_SPLIT_0;
import static com.def.GenericDef.PROC_EMPTY;
import static com.def.GenericDef.PROC_RELS;
import static com.def.GenericDef.PROC_RMPI;
import static com.def.GenericDef.PROC_RMWI;
import static com.def.GenericDef.PROC_SHTC;
import static com.def.GenericDef.PROC_WAIT;
import static com.def.GenericDef.PROC_WFRL;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPRMA;
import static com.def.GenericDef.WO_STATUS_CLOS;
import static com.def.GenericDef._ERROR;
import static com.def.GenericDef._NO;
import static com.def.GenericDef._NO1_50;
import static com.def.GenericDef._NORMAL;
import static com.def.GenericDef._OK;
import static com.def.GenericDef._YES;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IBisMdlDefDao;
import com.dao.IBisOpeDao;
import com.dao.IBisPathDao;
import com.dao.IBisPathiDao;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdGrdInfoDao;
import com.dao.IRetPrdInDao;
import com.dao.IRetPrdIndtDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdInfoShtcDao;
import com.dao.IRetPrdJgeDao;
import com.dao.IRetPrdLotDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Bis_mdl_def;
import com.model.Bis_ope;
import com.model.Bis_opeId;
import com.model.Bis_path;
import com.model.Bis_pathId;
import com.model.Bis_pathi;
import com.model.Bis_pathiId;
import com.model.Ret_box;
import com.model.Ret_prd_grd_info;
import com.model.Ret_prd_in;
import com.model.Ret_prd_indt;
import com.model.Ret_prd_indtId;
import com.model.Ret_prd_info;
import com.model.Ret_prd_info_shtc;
import com.model.Ret_prd_lot;
import com.model.Ret_prd_lotId;
import com.model.Ret_wo;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Xprma.XprmaI;
import com.tx.Xprma.XprmaIA;
import com.tx.Xprma.XprmaO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XprmaService")
public class XprmaService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XprmaService.class);
	private static String TX_NAME = T_XPRMA;

	private XprmaI inTrx;
	private XprmaO outTrx;
	private MqOpe mqOpe;
	
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Autowired
	private IRetPrdIndtDao retPrdIndtDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetPrdLotDao retPrdLotDao;
	@Autowired
	private IRetPrdJgeDao retPrdJgeDao;
	@Autowired
	private IRetPrdGrdInfoDao retPrdGrdInfoDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Autowired
	private IRetPrdInfoShtcDao retPrdInfoShtcDao;
	@Autowired
	private IBisOpeDao bisOpeDao;


	private String gszMsgBuf = "";

	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;

	public String subMainProc(String strInMsg) {
		Session session = retPrdInfoDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = null;

		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XprmaI();
		outTrx = new XprmaO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);

		mqOpe = new MqOpe();

		try {
			inTrx = (XprmaI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}

	/**
	 * Real action type
	 * 
	 * @param actionType
	 * @return
	 */
	private long subMainProc2(char actionType) {
		long lRc = _NORMAL;
		switch (actionType) {
		case 'I': {
			lRc = QueryReleaseRmaPrdFunc(inTrx.getWo_id_fk());
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'R': {//释放
			lRc = releasePrdFunc();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'C': {//取消释放
			lRc = cancelReleasePrdFunc();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'A': {//产品转入
			lRc = prdInBoxFunc();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'B': {//产品取消转入
			lRc = cancelPrdInFunc();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'U': {//产品提配
			lRc = unpackPrdFunc();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		case 'M': {//Get Material Box
			lRc = getMtrlBoxInfo();
			if (lRc != _NORMAL) {
				return lRc;
			}
			break;
		}
		default: {
			gszMsgBuf = "Invalide type input: [" + actionType + "]";
			return E_XPWMSOPE_INVALID_INPUT;
		}
		}

		return lRc;
	}
	
	private long QueryReleaseRmaPrdFunc(String wo_id){
		long lRc = _NORMAL;
		String mtrlBox = inTrx.getMtrl_box_id();
		String sql = "FROM Ret_prd_info WHERE wo_id_fk = '" + wo_id +"' AND prd_stat in ('RMWI','RMPI')";
		if(!Util.isSpaceCheck(mtrlBox)){
			sql = sql + " AND mtrl_box_id_fk = '" + mtrlBox + "'";
		}
			sql = sql +	" order by ACT_STB_TIMESTAMP DESC";
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		prdInfoList = retPrdInfoDao.find(sql);
		if(!prdInfoList.isEmpty()){
			outTrx.setPrd_cnt(prdInfoList.size());
			outTrx.setOary(prdInfoList);
		}
		
		//获取客户代码
		Ret_wo ret_wo = retWoDao.getWithLock(inTrx.getWo_id_fk(), LockMode.UPGRADE);
		if(ret_wo == null){
			gszMsgBuf = "内部订单 [" + inTrx.getWo_id_fk() + "] 不存在!";
			return E_RET_WO + E_READ_NOT_FOUND;
		}
		outTrx.setCus_id(ret_wo.getCus_id_fk());
		return lRc;
	}
	private long releasePrdFunc(){
		long lRc = _NORMAL;
		Integer pnl_std_pnl = 0;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Bis_mdl_def bis_mdl_def = new Bis_mdl_def();
		Ret_prd_in ret_prd_in = new Ret_prd_in();
		int prd_cnt = 0;
		String opeId = inTrx.getOpe_id_fk();
		String opeVer = inTrx.getOpe_ver_fk();
		String toolId = inTrx.getTool_id_fk();
		String procId = inTrx.getProc_id_fk();
		String toolgId = inTrx.getToolg_id_fk();
		/**
		 * 查询内部订单
		 */
		Ret_wo ret_wo = new Ret_wo();
		ret_wo = retWoDao.getWithLock(inTrx.getWo_id_fk(), LockMode.UPGRADE);
		if (null == ret_wo) {
			gszMsgBuf = "内部订单 [" + inTrx.getWo_id_fk() + "] 不存在!";
			return E_RET_WO + E_READ_NOT_FOUND;
		} else {
			if (WO_STATUS_CLOS.equals(ret_wo.getWo_stat())) {
				gszMsgBuf = "内部订单 [" + ret_wo.getWo_id() + "] 已经关闭!";
				return E_XPRMA_INVALID_WO_STATUS;
			}
			if(Util.isSpaceCheck(ret_wo.getSo_id())){
				gszMsgBuf = "内部订单 [" + ret_wo.getWo_id() + "] 未绑定商务订单!";
				return E_XPRMA_NO_SO;
			}
		}
		
		bis_mdl_def = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
		if (null == bis_mdl_def) {
			gszMsgBuf = " Not found record [" + ret_wo.getMdl_id_fk()
					+ "] in ["
					+ Bis_mdl_def.class.getSimpleName().toUpperCase() + "]!";
			return E_BIS_MDL_DEF + E_READ_NOT_FOUND;
		}

		rtnBeen = retPrdInDao.getRetPrdInByWoAndBox(inTrx.getWo_id_fk(), inTrx.getMtrl_box_id());
		if (E_RET_PRD_IN + E_READ_NOT_FOUND == rtnBeen.getlRc()) {
			gszMsgBuf = "原料箱:[" + inTrx.getMtrl_box_id()	+ "]不属于此工单:[" + inTrx.getWo_id_fk() + "]";
			return rtnBeen.getlRc();
		} else {
			ret_prd_in = (Ret_prd_in) rtnBeen.getObj();
		}
		if (inTrx.getTbl_cnt_a() > ret_prd_in.getPrd_act_qty()) {
			gszMsgBuf = "Mtrl box[" + ret_prd_in.getId().getMtrl_box_id()
					+ "] is not panel to use.";
			return E_XPRMA_PRD_IN_NOT_ENOUGH;
		}
		prd_cnt = (int) inTrx.getTbl_cnt_a();
		if (PROC_WFRL.equals(ret_prd_in.getPpbox_stat())) {
			if (prd_cnt == ret_prd_in.getPrd_act_qty()) {
				ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty() + 1);
				ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty() - 1);
				ret_prd_in.setPpbox_stat(PROC_RELS);
				ret_prd_in.setEvt_cate(EVT_STB_INPR);
				ret_prd_in.setEvt_usr(inTrx.getEvt_usr());
				retPrdInDao.update(ret_prd_in);
				
				ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_tl_qty());
				ret_prd_in.setPrd_act_qty(0);
				ret_prd_in.setPpbox_stat(PROC_EMPTY);
				
			}else{
				ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty() + prd_cnt);
				ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty() - prd_cnt);
				ret_prd_in.setPpbox_stat(PROC_RELS);
			}
			
		}else if (PROC_RELS.equals(ret_prd_in.getPpbox_stat())) {
			ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty() + prd_cnt);
			ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty() - prd_cnt);
			if (0 == ret_prd_in.getPrd_act_qty()) {
				ret_prd_in.setPpbox_stat(PROC_EMPTY);
			}
			
		} else {
			gszMsgBuf = "箱子 [" + inTrx.getMtrl_box_id() + "] 未发料，请先发料";
			return E_XPRMA_MTRL_BOX_INVALIED;
		}

		ret_prd_in.setEvt_cate(EVT_STB_INPR);
		ret_prd_in.setEvt_usr(inTrx.getEvt_usr());
		retPrdInDao.update(ret_prd_in);		
		
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		List<Ret_prd_grd_info> prdGrdInfoList = new ArrayList<Ret_prd_grd_info>();
		
		for(int i=0; i<inTrx.getTbl_cnt_a(); i++){
			Ret_prd_grd_info prd_grd_info = new Ret_prd_grd_info();
			List<XprmaIA> iaryA = new ArrayList<XprmaIA>();
			iaryA = inTrx.getIaryA();
			String prdId = iaryA.get(i).getPrd_seq_id_fk();
			String fabSn = prdId;
			Ret_prd_info_shtc prdInfoShtc = new Ret_prd_info_shtc();
			Ret_prd_info ret_prd_info = retPrdInfoDao.get(iaryA.get(i).getPrd_seq_id_fk());
			if(ret_prd_info != null){
				if(Util.isSpaceCheck(ret_prd_info.getPrd_stat()) || !PROC_SHTC.equals(ret_prd_info.getPrd_stat())){
					gszMsgBuf = "产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的产品状态为[" +
							ret_prd_info.getPrd_stat()+"],应该为出货状态[SHTC]！";
					return E_XPRMA_INVALID_STATUS;
				}
				if(ret_prd_info.getWo_id_fk().equals(inTrx.getWo_id_fk())){
					gszMsgBuf = "玻璃重复[" + prdId + "]!";
					return E_XPRMA_INVALID_STATUS;
				}
                prdId = prdId + "_" + inTrx.getWo_id_fk();				
			}
			ret_prd_info = new Ret_prd_info();
			
			
			lRc = addPrdIndtForUnpack(ret_prd_in ,prdId, i);
			if (lRc != _NORMAL) {
				return lRc;
			}

			prdInfoShtc = retPrdInfoShtcDao.get(iaryA.get(i).getPrd_seq_id_fk());
			if(prdInfoShtc == null){
				ret_prd_info.setExt_1("N");
				ret_prd_info.setPrd_seq_id(prdId);
				ret_prd_info.setFab_sn(fabSn);
			}else{
				ret_prd_info.setExt_1("R");
				ret_prd_info.setPrd_seq_id(prdId);
				ret_prd_info.setFab_sn(fabSn);
			}
			ret_prd_info.setWo_id_fk(inTrx.getWo_id_fk());
			ret_prd_info.setSo_id_fk(ret_wo.getSo_id());
			ret_prd_info.setPrd_seq_typ("S");
			ret_prd_info.setLot_splt_id("00");
			ret_prd_info.setMdl_cate_fk(bis_mdl_def.getMdl_cate());
			ret_prd_info.setMdl_typ_fk(bis_mdl_def.getMdl_typ());
			ret_prd_info.setMdl_id_fk(bis_mdl_def.getMdl_id());
			ret_prd_info.setLayot_id_fk(inTrx.getLayot_id());
			ret_prd_info.setX_axis_cnt_fk(Integer.valueOf(inTrx.getX_axis_cnt()));
			ret_prd_info.setY_axis_cnt_fk(Integer.valueOf(inTrx.getY_axis_cnt()));
			pnl_std_pnl = ret_prd_info.getX_axis_cnt_fk() * ret_prd_info.getY_axis_cnt_fk();
			ret_prd_info.setPrd_qty(pnl_std_pnl);
			ret_prd_info.setPrd_std_qty(pnl_std_pnl);
			ret_prd_info.setPrd_thickness(ret_wo.getTo_thickness());
			ret_prd_info.setPrd_grade(_OK);
			ret_prd_info.setPv_prd_grade(_OK);
			ret_prd_info.setPrd_stat(PROC_RMWI);
			ret_prd_info.setEvt_cate(EVT_RMA_RELEASE);
			ret_prd_info.setEvt_seq_id(gEvtSeqID);
			ret_prd_info.setEvt_time_d(gEvtTimeD);
			ret_prd_info.setEvt_timestamp(gEvtTimestamp);
			ret_prd_info.setAct_stb_timestamp(gEvtTimestamp);
			ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_info.setMtrl_box_id_fk(ret_prd_info.getBox_id_fk());
			ret_prd_info.setBox_id_fk("");
			//ret_prd_info.setDn_no("");
			ret_prd_info.setOqc_skip_flg("N");
			ret_prd_info.setWh_in_timestamp(null);
			ret_prd_info.setShipping_timestamp(null);
			ret_prd_info.setShip_box_id_fk("");			
			ret_prd_info.setMtrl_box_id_fk(inTrx.getMtrl_box_id());
			ret_prd_info.setSlot_no(iaryA.get(i).getSlot_no());
			ret_prd_info.setOqc_skip_flg("");
			//保存cr path&ope
			ret_prd_info.setSv_path_id_fk(ret_prd_info.getCr_path_id_fk());
			ret_prd_info.setSv_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
			ret_prd_info.setSv_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
			ret_prd_info.setSv_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
			ret_prd_info.setSv_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
			ret_prd_info.setPv_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
			ret_prd_info.setPv_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
			ret_prd_info.setPv_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
			//设定产品的new cr path&ope虚拟途程和站点
			ret_prd_info.setCr_path_id_fk("BSTB001");
			ret_prd_info.setCr_path_ver_fk("00000");
			ret_prd_info.setCr_ope_no_fk(opeId);
			ret_prd_info.setCr_ope_id_fk(opeId);
			ret_prd_info.setCr_ope_ver_fk(opeVer);
			ret_prd_info.setCr_proc_id_fk(procId);
			ret_prd_info.setCr_toolg_id_fk(toolgId);
			ret_prd_info.setCr_tool_id_fk(toolId);
			ret_prd_info.setGroup_id("");
			
			ret_prd_info.setBnk_flg(BANK_OUT);
			ret_prd_info.setPrd_thickness(ret_wo.getTo_thickness());
			ret_prd_info.setPrd_admin_flg("NNNNNNNNNNNNNNNNNNNN");

			prdInfoList.add(ret_prd_info);
			
			//新增prd grd info表
			String prd_seq_grd = "";
			String prd_seq_grd_t = "";
			String prd_seq_grd_c = "";
			prd_grd_info.setPrd_seq_id_fk(prdId);
			prd_grd_info.setMdl_id_fk(ret_wo.getMdl_id_fk());
			for(int j=0 ; j<pnl_std_pnl; j++){
				prd_seq_grd = prd_seq_grd + "O";
				prd_seq_grd_t = prd_seq_grd_t + "O";
				prd_seq_grd_c = prd_seq_grd_c + "O";
			}
			prd_grd_info.setPrd_seq_grd(prd_seq_grd);
			prd_grd_info.setPrd_seq_grd_t(prd_seq_grd_t);
			prd_grd_info.setPrd_seq_grd_c(prd_seq_grd_c);
			prd_grd_info.setEvt_usr(inTrx.getEvt_usr());
			prd_grd_info.setEvt_timestamp(gEvtTimestamp);
			prd_grd_info.setPrd_qty(pnl_std_pnl);
			prd_grd_info.setPrd_std_qty(pnl_std_pnl);
			prd_grd_info.setLayot_id_fk(inTrx.getLayot_id());
			prd_grd_info.setX_axis_cnt_fk(Integer.valueOf(inTrx.getX_axis_cnt()));
			prd_grd_info.setY_axis_cnt_fk(Integer.valueOf(inTrx.getY_axis_cnt()));
			prd_grd_info.setCus_ng_qty(0);
			prd_grd_info.setProc_ng_qty(0);
			prd_grd_info.setAct_cus_ng_qty(0);
			prd_grd_info.setAct_proc_ng_qty(0);
			prdGrdInfoList.add(prd_grd_info);
	
		}
		retPrdInfoDao.saveOrUpdateAll(prdInfoList);
		retPrdGrdInfoDao.saveOrUpdateAll(prdGrdInfoList);
		//更新内部订单信息
		if (0 == ret_wo.getRl_prd_qty()) {
			ret_wo.setAct_stb_timestamp(gEvtTimestamp);
		}
		ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty() + prdInfoList.size());
		if(ret_wo.getPln_prd_qty()<ret_wo.getRl_prd_qty()){
			gszMsgBuf = "内部订单[" + ret_wo.getWo_id() + "]计划数量[" + ret_wo.getPln_prd_qty() +
					"]不够释放数量[" + ret_wo.getRl_prd_qty() + "]";
			return E_XPRMA_INVALID_STATUS;
		}
		retWoDao.update(ret_wo);
		
		QueryReleaseRmaPrdFunc(inTrx.getWo_id_fk());
		outTrx.setBox_can_use_qty(ret_prd_in.getPrd_act_qty());
		return lRc;
	}
	
	private long cancelReleasePrdFunc(){
		long lRc = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		String wo_id="", mtrl_box_id = "";
		int pnlCnt = 0;
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		for(int i=0; i<inTrx.getTbl_cnt_a(); i++){
			String delflg = "N";
			List<XprmaIA> iaryA = new ArrayList<XprmaIA>();
			iaryA = inTrx.getIaryA();
			Ret_prd_info ret_prd_info = new Ret_prd_info();
			Ret_prd_info_shtc prdInfoShtc = new Ret_prd_info_shtc();
			ret_prd_info = retPrdInfoDao.get(iaryA.get(i).getPrd_seq_id_fk());
			if(ret_prd_info == null){
				gszMsgBuf = "找不到产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的信息,请确认！";
				return E_READ_NOT_FOUND + E_RET_PRD_INFO;
			}else{
				if(Util.isSpaceCheck(ret_prd_info.getPrd_stat()) ||
						!PROC_RMWI.equals(ret_prd_info.getPrd_stat())){
					gszMsgBuf = "产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的产品状态为[" +
						ret_prd_info.getPrd_stat()+"],应该为等待产品转入状态[RMWI]！";
					return E_XPRMA_INVALID_STATUS;
				}
				if (!Util.isSpaceCheck(ret_prd_info.getBox_id_fk())){
					gszMsgBuf = "产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的产品已转入箱为[" +
							ret_prd_info.getBox_id_fk()+"],请先取消转入！";
						return E_XPRMA_INVALID_STATUS;
				}
				wo_id = ret_prd_info.getWo_id_fk();
				mtrl_box_id = ret_prd_info.getMtrl_box_id_fk();
				if (iaryA.get(i).getExt_1().equals("R")){
					prdInfoShtc = retPrdInfoShtcDao.get(iaryA.get(i).getPrd_seq_id_fk());
					if(prdInfoShtc == null){
						prdInfoShtc = retPrdInfoShtcDao.get(ret_prd_info.getFab_sn());
						if(prdInfoShtc == null){
						   gszMsgBuf = "找不到产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的出货信息,请确认！";
						   return E_READ_NOT_FOUND + E_RET_PRD_INFO_SHTC;
						}else{
							delflg = "Y";
						}
					}
				}else{
					delflg = "Y";
				}	
				if(delflg.equals("N")){
					ret_prd_info.setWo_id_fk(prdInfoShtc.getWo_id_fk());
					ret_prd_info.setMtrl_box_id_fk(prdInfoShtc.getMtrl_box_id_fk());
					ret_prd_info.setBox_id_fk(prdInfoShtc.getBox_id_fk());
					ret_prd_info.setSlot_no(prdInfoShtc.getSlot_no());
					ret_prd_info.setPrd_stat(PROC_SHTC);
					ret_prd_info.setEvt_cate(EVT_RMA_CANCEL_RELEASE);
					ret_prd_info.setEvt_seq_id(gEvtSeqID);
					ret_prd_info.setEvt_time_d(gEvtTimeD);
					ret_prd_info.setEvt_timestamp(gEvtTimestamp);
					ret_prd_info.setEvt_usr(inTrx.getEvt_usr());

					
					//重置cr path&ope
					
					ret_prd_info.setCr_path_id_fk(ret_prd_info.getSv_path_id_fk());
					ret_prd_info.setCr_path_ver_fk(ret_prd_info.getSv_path_ver_fk());
					ret_prd_info.setCr_ope_no_fk(ret_prd_info.getSv_ope_no_fk());
					ret_prd_info.setCr_ope_id_fk(ret_prd_info.getSv_ope_id_fk());
					ret_prd_info.setCr_ope_ver_fk(ret_prd_info.getSv_ope_ver_fk());
					ret_prd_info.setCr_proc_id_fk(ret_prd_info.getPv_proc_id_fk());
					ret_prd_info.setCr_toolg_id_fk(ret_prd_info.getPv_toolg_id_fk());
					ret_prd_info.setCr_tool_id_fk(ret_prd_info.getPv_tool_id_fk());
					ret_prd_info.setSv_path_id_fk("");
					ret_prd_info.setSv_path_ver_fk("");
					ret_prd_info.setSv_ope_no_fk("");
					ret_prd_info.setSv_ope_id_fk("");
					ret_prd_info.setSv_ope_ver_fk("");
					ret_prd_info.setPv_proc_id_fk("");
					ret_prd_info.setPv_toolg_id_fk("");
					ret_prd_info.setPv_tool_id_fk("");
					
					ret_prd_info.setBnk_flg(BANK_IN);
					
					prdInfoList.add(ret_prd_info);
				}else{
					retPrdInfoDao.delete(ret_prd_info);
				}
				lRc = resetRetPrdInForCancelUnpack(wo_id, mtrl_box_id);
				if (lRc != _NORMAL) {
					return lRc;
				}
				
				lRc = delRetPrdIndtForCancelUnpack(inTrx.getIaryA().get(i).getPrd_seq_id_fk());
				if (lRc != _NORMAL) {
					return lRc;
				}
				
				lRc = delRetGrdInfoForCancelUnpack(inTrx.getIaryA().get(i).getPrd_seq_id_fk());
				if (lRc != _NORMAL) {
					return lRc;
				}
				
				rtnBeen = retPrdJgeDao.delAllDefByPrdId(ret_prd_info.getPrd_seq_id());
				if (_NORMAL != rtnBeen.getlRc()) {
					return rtnBeen.getlRc();
				}
				pnlCnt ++;
			}		
		}
		retPrdInfoDao.saveOrUpdateAll(prdInfoList);
		/**
		 * 更新内部订单
		 */
		Ret_wo ret_wo = new Ret_wo();
		ret_wo = retWoDao.getWithLock(wo_id, LockMode.UPGRADE);
		if (null == ret_wo) {
			gszMsgBuf = "内部订单 [" + wo_id + "] 不存在!";
			return E_RET_WO + E_READ_NOT_FOUND;
		} else {
			if (WO_STATUS_CLOS.equals(ret_wo.getWo_stat())) {
				gszMsgBuf = "内部订单 [" + ret_wo.getWo_id() + "] 已经关闭!";
				return E_XPRMA_INVALID_WO_STATUS;
			}
			ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty() - pnlCnt);
			retWoDao.update(ret_wo);
		}
		

		return lRc;
	}
	
	private long prdInBoxFunc(){
		long lRc = _NORMAL;
		/**
		 * 查询转入箱信息
		 */
		Ret_box ret_box = new Ret_box();
		ret_box = retBoxDao.get(inTrx.getBox_id_fk());
		if (null == ret_box) {
			gszMsgBuf = "箱子[" + inTrx.getBox_id_fk() + "]，系统中不存在,请新建箱号！";
			return E_READ_NOT_FOUND + E_RET_BOX;
		}
		
		if (!ret_box.getBox_stat().equals(PROC_RMPI) && !ret_box.getBox_stat().equals(PROC_EMPTY)) {
			gszMsgBuf = "箱子[" + inTrx.getBox_id_fk() + "]状态["+ret_box.getBox_stat()+"]不符，必须为EMPT或RMPI！";
			return E_READ_NOT_FOUND + E_RET_BOX;
		}
		
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		for(int i=0; i<inTrx.getTbl_cnt_a(); i++){
			List<XprmaIA> iaryA = new ArrayList<XprmaIA>();
			iaryA = inTrx.getIaryA();
			Ret_prd_info ret_prd_info = new Ret_prd_info();
			ret_prd_info = retPrdInfoDao.get(iaryA.get(i).getPrd_seq_id_fk());
			if(ret_prd_info == null){
				gszMsgBuf = "找不到产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的信息,请确认！";
				return E_READ_NOT_FOUND + E_RET_PRD_INFO;
			}else{
				if(Util.isSpaceCheck(ret_prd_info.getPrd_stat()) ||
						!PROC_RMWI.equals(ret_prd_info.getPrd_stat())){
					gszMsgBuf = "产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的产品状态为[" +
						ret_prd_info.getPrd_stat()+"],应该为等待产品转入状态[RMWI]！";
					return E_XPRMA_INVALID_STATUS;
				}
				if(!Util.isSpaceCheck(ret_box.getWo_id_fk()) &&
					!ret_prd_info.getWo_id_fk().equals(ret_box.getWo_id_fk())){
					gszMsgBuf = "产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "]的内部订单为[" +
						ret_prd_info.getWo_id_fk()+"]VS箱子的内部订单["+ ret_box.getWo_id_fk() +"]！";
					return E_XPRMA_INVALID_WO;
				}
				if (retPrdInfoDao.hasUserInputSlot(inTrx.getBox_id_fk(),
						iaryA.get(i).getSlot_no())) {
					gszMsgBuf = "子格位[" + iaryA.get(i).getSlot_no() + "]在箱子["
							+ inTrx.getBox_id_fk() + "]中已经存在！";
					return E_XPRMA_SLOT_NO_EXIST;
				}
				ret_prd_info.setPrd_stat(PROC_RMPI);
				ret_prd_info.setEvt_cate(EVT_RMA_PRD_IN);
				ret_prd_info.setEvt_seq_id(gEvtSeqID);
				ret_prd_info.setEvt_time_d(gEvtTimeD);
				ret_prd_info.setEvt_timestamp(gEvtTimestamp);
				ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_info.setBox_id_fk(inTrx.getBox_id_fk());
//				ret_prd_info.setSlot_no(retPrdInfoDao.getNxSlotNoLoop(inTrx.getBox_id_fk()));
				prdInfoList.add(ret_prd_info);
			}	
		}
		retPrdInfoDao.saveOrUpdateAll(prdInfoList);
		/**
		 * 获取内部订单信息
		 */
		Ret_wo ret_wo = new Ret_wo();
		ret_wo = retWoDao.getWithLock(prdInfoList.get(0).getWo_id_fk(), LockMode.UPGRADE);
		if (null == ret_wo) {
			gszMsgBuf = "内部订单 [" + prdInfoList.get(0).getWo_id_fk() + "] 不存在!";
			return E_RET_WO + E_READ_NOT_FOUND;
		} else {
			if (WO_STATUS_CLOS.equals(ret_wo.getWo_stat())) {
				gszMsgBuf = "内部订单 [" + ret_wo.getWo_id() + "] 已经关闭!";
				return E_XPRMA_INVALID_WO_STATUS;
			}
		}
		
		/**
		 * 获取产品型号信息
		 */
		Bis_mdl_def bis_mdl_def = new Bis_mdl_def();
		if(!Util.isSpaceCheck(ret_wo.getMdl_id_fk())){
			bis_mdl_def =  bisMdlDefDao.get(ret_wo.getMdl_id_fk());
			if (bis_mdl_def == null) {
				gszMsgBuf = "找不到产品型号["+ ret_wo.getMdl_id_fk() +"]的信息，请确认!";
				return E_RET_WO + E_BIS_MDL_DEF;
			}
		}else{
			gszMsgBuf = "内部订单[" + ret_wo.getWo_id() + "]的产品型号为["+ ret_wo.getMdl_id_fk() +"]!";
			return E_XPRMA_MDL_ID_NULL;
		}
		Integer prd_qty =  ret_box.getPrd_qty() + prdInfoList.size();
		if(ret_box.getStd_qty() < prd_qty){
			gszMsgBuf = "箱子[" + ret_box.getBox_id() + "]的标准数量[" +
				ret_box.getStd_qty()+"] VS 转入产品后数量["+ prd_qty +"]！";
			return E_XPRMA_OVER_STD_CNT;
		}
		ret_box.setBox_stat(PROC_RMPI);
		ret_box.setReal_emp(_NO);
		ret_box.setPrd_qty(prd_qty);
		ret_box.setMdl_typ_fk(bis_mdl_def.getMdl_typ());
		ret_box.setMdl_id_fk(bis_mdl_def.getMdl_id());
		ret_box.setMdl_cate_fk(ret_wo.getMdl_cate_fk());
		ret_box.setPrty(ret_wo.getWo_prty());
		ret_box.setWo_id_fk(ret_wo.getWo_id());
		ret_box.setEvt_cate(EVT_RMA_PRD_IN);
		ret_box.setEvt_time_d(gEvtTimeD);
		ret_box.setEvt_timestamp(gEvtTimestamp);
		ret_box.setEvt_usr(inTrx.getEvt_usr());
		retBoxDao.update(ret_box);
		return lRc;
	}
	
	private long cancelPrdInFunc(){
		long lRc = _NORMAL;
		String box_id="";
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		for(int i=0; i<inTrx.getTbl_cnt_a(); i++){
			List<XprmaIA> iaryA = new ArrayList<XprmaIA>();
			iaryA = inTrx.getIaryA();
			Ret_prd_info ret_prd_info = new Ret_prd_info();
			ret_prd_info = retPrdInfoDao.get(iaryA.get(i).getPrd_seq_id_fk());
			if(ret_prd_info == null){
				gszMsgBuf = "找不到产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的信息,请确认！";
				return E_READ_NOT_FOUND + E_RET_PRD_INFO;
			}else{
				if(Util.isSpaceCheck(ret_prd_info.getPrd_stat()) ||
						!PROC_RMPI.equals(ret_prd_info.getPrd_stat())){
					gszMsgBuf = "产品ID[" + iaryA.get(i).getPrd_seq_id_fk() + "的产品状态为[" +
						ret_prd_info.getPrd_stat()+"],应该为产品转入状态[RMPI]！";
					return E_XPRMA_INVALID_STATUS;
				}
				box_id = ret_prd_info.getBox_id_fk();
				ret_prd_info.setPrd_stat(PROC_RMWI);
				ret_prd_info.setEvt_cate(EVT_RMA_CANCEL_PRD_IN);
				ret_prd_info.setEvt_seq_id(gEvtSeqID);
				ret_prd_info.setEvt_time_d(gEvtTimeD);
				ret_prd_info.setEvt_timestamp(gEvtTimestamp);
				ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_info.setBox_id_fk("");
//				ret_prd_info.setSlot_no("");
				prdInfoList.add(ret_prd_info);
			}	
			/**
			 * 查询转入箱信息
			 */
			Ret_box ret_box = new Ret_box();
			ret_box = retBoxDao.get(box_id);
			if (null == ret_box) {
				gszMsgBuf = "箱子[" + box_id + "]，系统中不存在,请确认！";
				return E_READ_NOT_FOUND + E_RET_BOX;
			}
			if(!PROC_RMPI.equals(ret_box.getBox_stat())){
				gszMsgBuf = "箱子[" + inTrx.getBox_id_fk() + "的状态为[" +
				ret_box.getBox_stat()+"],应该为产品转入状态[RMPI]！";
			}
			Integer prd_qty = ret_box.getPrd_qty() - 1;
			if(prd_qty == 0){
				ret_box.setBox_stat(PROC_EMPTY);
				ret_box.setReal_emp(_YES);
				ret_box.setMdl_typ_fk(null);
				ret_box.setMdl_id_fk(null);
				ret_box.setMdl_cate_fk(null);
				ret_box.setPrty(null);
				ret_box.setWo_id_fk(null);
			}
			ret_box.setPrd_qty(prd_qty);
			ret_box.setEvt_cate(EVT_RMA_CANCEL_PRD_IN);
			ret_box.setEvt_time_d(gEvtTimeD);
			ret_box.setEvt_timestamp(gEvtTimestamp);
			ret_box.setEvt_usr(inTrx.getEvt_usr());
			retBoxDao.update(ret_box);
		}
		retPrdInfoDao.saveOrUpdateAll(prdInfoList);
		QueryReleaseRmaPrdFunc(prdInfoList.get(0).getWo_id_fk());
		return lRc;
	}
	
	private long unpackPrdFunc(){
		long lRc = _NORMAL;
		String path_id="",path_ver="";
		int woUpdFlg = 0;
		Bis_pathiId bis_pathi_id = new Bis_pathiId();
		/**
		 * 箱子信息
		 */
		Ret_box ret_box = new Ret_box();
		ret_box = retBoxDao.get(inTrx.getBox_id_fk());
		if (null == ret_box) {
			gszMsgBuf = "箱子[" + inTrx.getBox_id_fk() + "]，系统中不存在,请确认！";
			return E_READ_NOT_FOUND + E_RET_BOX;
		}
		if(!PROC_RMPI.equals(ret_box.getBox_stat())){
			gszMsgBuf = "箱子[" + inTrx.getBox_id_fk() + "的状态为[" +
			ret_box.getBox_stat()+"],应该为产品转入状态[RMPI]！";
			return E_READ_NOT_FOUND + E_RET_BOX;
		}
		if (!ret_box.getWo_id_fk().equals(inTrx.getWo_id_fk())) {
			gszMsgBuf = "箱子[" + inTrx.getBox_id_fk() + "的工单为[" +
			ret_box.getWo_id_fk()+"]跟当前工单["+inTrx.getWo_id_fk()+"]不匹配！";
			return E_READ_NOT_FOUND + E_RET_BOX;
		}
		/**
		 * 内部订单信息
		 */
		Ret_wo ret_wo = new Ret_wo();
		ret_wo = retWoDao.getWithLock(inTrx.getWo_id_fk(), LockMode.UPGRADE);
		if (null == ret_wo) {
			gszMsgBuf = "内部订单 [" + inTrx.getWo_id_fk() + "] 不存在!";
			return E_RET_WO + E_READ_NOT_FOUND;
		} else {
			if (WO_STATUS_CLOS.equals(ret_wo.getWo_stat())) {
				gszMsgBuf = "内部订单 [" + ret_wo.getWo_id() + "] 已经关闭!";
				return E_XPRMA_INVALID_WO_STATUS;
			}
		}
		lRc = checkLotInfo();
		if (lRc != _NORMAL) {
			return lRc;
		}
		/**
		 * 查询返工工艺路线信息
		 */
		if (!Util.isSpaceCheck(ret_wo.getPath_id_fk())) {
			path_id = ret_wo.getPath_id_fk();
			path_ver = ret_wo.getPath_ver_fk();
		}else {
			if (inTrx.getPath_id_fk() == null || Util.isSpaceCheck(inTrx.getPath_id_fk())) {
				gszMsgBuf = "内部订单 [" + ret_wo.getWo_id() + "] 尚未绑定工艺路线，请选择!";
				return E_XPRMA_INVALID_WO_STATUS;
			}
			path_id = inTrx.getPath_id_fk();
			path_ver = "00000";
			woUpdFlg = 1;
		}
		
		bis_pathi_id.setPath_id(path_id);
		bis_pathi_id.setPath_ver(path_ver);
		Bis_pathi bis_pathi = bisPathiDao.get(bis_pathi_id); 
		if(bis_pathi == null){
			gszMsgBuf = "找不到工艺路线[" + inTrx.getPath_id_fk() + "][00000]的信息，请先创建工艺路线！";
			return E_READ_NOT_FOUND + E_BIS_PATHI;
		}
		
		Bis_pathId bis_path_id = new Bis_pathId();
		bis_path_id.setCr_ope_no(bis_pathi.getFirst_ope_no());
		bis_path_id.setPath_id_fk(bis_pathi.getId().getPath_id());
		bis_path_id.setPath_ver_fk(bis_pathi.getId().getPath_ver());
		Bis_path bis_path = bisPathDao.get(bis_path_id);
		if(bis_path == null){
			gszMsgBuf = "工艺路线[" + bis_pathi.getId().getPath_id() + "][" 
				+ bis_pathi.getId().getPath_ver() + "]未维护站点信息,请先创建工艺路线的站点信息！";
			return E_READ_NOT_FOUND + E_BIS_PATH;
		}
		if ( woUpdFlg == 1 ) {
			ret_wo.setPath_id_fk(bis_pathi.getId().getPath_id());
			ret_wo.setPath_ver_fk(bis_pathi.getId().getPath_ver());
			ret_wo.setStr_ope_no(bis_pathi.getFirst_ope_no());
			retWoDao.update(ret_wo);
		}
		
		Bis_pathId bis_path_id_snd = new Bis_pathId();
		bis_path_id_snd.setCr_ope_no(bis_path.getNx_ope_no());
		bis_path_id_snd.setPath_id_fk(bis_pathi.getId().getPath_id());
		bis_path_id_snd.setPath_ver_fk(bis_pathi.getId().getPath_ver());
		Bis_path bis_path_snd = bisPathDao.get(bis_path_id_snd);
		if(bis_path_snd == null){
			gszMsgBuf = "工艺路线[" + bis_pathi.getId().getPath_id() + "][" 
				+ bis_pathi.getId().getPath_ver() + "]未维护第二站点信息,请先创建工艺路线的站点信息！";
			return E_READ_NOT_FOUND + E_BIS_PATH;
		}
		/**
		 * 获取tool group and proc id
		 */
		Bis_ope bis_ope = new Bis_ope();
		Bis_opeId bis_opeId = new Bis_opeId();
		bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
		bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());

		bis_ope = bisOpeDao.get(bis_opeId);
		if (bis_ope == null) {
			gszMsgBuf = "找不到站点["+bis_path.getCr_ope_id_fk()+"]["+bis_path.getCr_ope_ver_fk()+"]信息！";
			return E_BIS_TOOL_GR + E_READ_NOT_FOUND;
		}

		Bis_ope bis_ope_snd = new Bis_ope();
		Bis_opeId bis_opeId_snd = new Bis_opeId();
		bis_opeId_snd.setOpe_id(bis_path_snd.getCr_ope_id_fk());
		bis_opeId_snd.setOpe_ver(bis_path_snd.getCr_ope_ver_fk());

		bis_ope_snd = bisOpeDao.get(bis_opeId_snd);
		if (bis_ope_snd == null) {
			gszMsgBuf = "找不到站点["+bis_path_snd.getCr_ope_id_fk()+"]["+bis_path_snd.getCr_ope_ver_fk()+"]信息！";
			return E_BIS_TOOL_GR + E_READ_NOT_FOUND;
		}
		
		/**
		 * 产品信息
		 */
		List<Ret_prd_info> ret_prd_infoList = retPrdInfoDao.getShtListInfo(inTrx.getBox_id_fk());
		if (null == ret_prd_infoList) {
			gszMsgBuf = "箱子 [" + inTrx.getBox_id_fk() + "]中没有产品,请检查 !";
			return E_RET_PRD_INFO + E_READ_NOT_FOUND;
		}
		
		List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
		for(int i=0; i<ret_prd_infoList.size(); i++){
			if(!PROC_RMPI.equals(ret_prd_infoList.get(i).getPrd_stat())){
				gszMsgBuf = "产品ID[" + ret_prd_infoList.get(i).getPrd_seq_id() + "的产品状态为[" +
					ret_prd_infoList.get(i).getPrd_stat()+"],应该为产品转入状态[RMPI]！";
				return E_XPRMA_INVALID_STATUS;
			}
			if ( ( i > 0 ) && !ret_prd_infoList.get(0).getPrd_grade().equals(ret_prd_infoList.get(i).getPrd_grade())) {
				gszMsgBuf = "产品ID[" + ret_prd_infoList.get(i).getPrd_seq_id() + "的产品等级为[" +
						ret_prd_infoList.get(i).getPrd_grade()+"],有混！产品ID[" + 
						ret_prd_infoList.get(0).getPrd_seq_id() + "的产品等级为[" +
						ret_prd_infoList.get(0).getPrd_grade()+"]";
					return E_XPRMA_INVALID_STATUS;
			}
			if(!ret_prd_infoList.get(0).getCr_ope_id_fk().equals(bis_path.getCr_ope_id_fk())){
				gszMsgBuf = "产品ID[" + ret_prd_infoList.get(i).getPrd_seq_id() + "]返工站点[" +
						ret_prd_infoList.get(i).getCr_ope_id_fk()+"]与工单工艺路线的首站点["+
						bis_path.getCr_ope_id_fk() + "]不一致";
				return E_XPRMA_INVALID_STATUS;
			}	
			ret_prd_infoList.get(i).setPrd_stat(PROC_WAIT);
			ret_prd_infoList.get(i).setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
			ret_prd_infoList.get(i).setNx_path_id_fk(bis_path.getId().getPath_id_fk());
			ret_prd_infoList.get(i).setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
		    ret_prd_infoList.get(i).setNx_ope_no_fk(bis_path_snd.getId().getCr_ope_no());
			ret_prd_infoList.get(i).setNx_ope_id_fk(bis_path_snd.getCr_ope_id_fk());
			ret_prd_infoList.get(i).setNx_ope_ver_fk(bis_path_snd.getCr_ope_ver_fk());
			ret_prd_infoList.get(i).setNx_proc_id_fk(bis_ope_snd.getProc_id());
			ret_prd_infoList.get(i).setNx_toolg_id_fk(bis_ope_snd.getToolg_id());			
			ret_prd_infoList.get(i).setSo_id_fk(ret_wo.getSo_id());
			ret_prd_infoList.get(i).setLot_id(inTrx.getLot_id());
			ret_prd_infoList.get(i).setFst_lot_id_fk(inTrx.getLot_id());
			ret_prd_infoList.get(i).setSnd_lot_id_fk(inTrx.getLot_id());
			ret_prd_infoList.get(i).setCus_sn(_NO1_50);
			ret_prd_infoList.get(i).setEvt_cate(EVT_RMA_STB_INPR);
			ret_prd_infoList.get(i).setEvt_seq_id(gEvtSeqID);
			ret_prd_infoList.get(i).setEvt_time_d(gEvtTimeD);
			ret_prd_infoList.get(i).setEvt_timestamp(gEvtTimestamp);
			ret_prd_infoList.get(i).setEvt_usr(inTrx.getEvt_usr());
			prdInfoList.add(ret_prd_infoList.get(i));
		}
		retPrdInfoDao.saveOrUpdateAll(prdInfoList);
		
		ret_box.setBox_stat(PROC_WAIT);
		ret_box.setPrd_grade_fk(ret_prd_infoList.get(0).getPrd_grade());
		ret_box.setMtrl_prod_id_fk(ret_wo.getMtrl_prod_id_fk());
		ret_box.setNx_path_id_fk(bis_path.getId().getPath_id_fk());
		ret_box.setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
		ret_box.setNx_ope_no_fk(bis_path_snd.getId().getCr_ope_no());
		ret_box.setNx_ope_id_fk(bis_path_snd.getCr_ope_id_fk());
		ret_box.setNx_ope_ver_fk(bis_path_snd.getCr_ope_ver_fk());
		ret_box.setNx_proc_id_fk(bis_ope_snd.getProc_id());
		ret_box.setNx_toolg_id_fk(bis_ope_snd.getToolg_id());
		ret_box.setSo_id_fk(ret_wo.getSo_id());
		ret_box.setLot_id_fk(inTrx.getLot_id());
		ret_box.setEvt_cate(EVT_RMA_STB_INPR);
		ret_box.setEvt_time_d(gEvtTimeD);
		ret_box.setEvt_timestamp(gEvtTimestamp);
		ret_box.setPrd_admin_flg(ret_prd_infoList.get(0).getPrd_admin_flg());
		ret_box.setEvt_usr(inTrx.getEvt_usr());
		retBoxDao.update(ret_box);

        lRc = addLotInfo(ret_box.getPrd_qty());	
        if (lRc != _NORMAL) {
        	return lRc;
        }
		return lRc;
	}
	
	/**
	 * 查询来料箱号信息
	 * 
	 * @return
	 */
	private long getMtrlBoxInfo() {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_prd_in ret_prd_in = new Ret_prd_in();
		List<Ret_prd_info_shtc> prdInfoSthcList = new ArrayList<Ret_prd_info_shtc>();

		rtnBeen = retPrdInDao.getRetPrdInByWoAndBox(inTrx.getWo_id_fk(),
				inTrx.getMtrl_box_id());
		if (E_RET_PRD_IN + E_READ_NOT_FOUND == rtnBeen.getlRc()) {
			gszMsgBuf = "内部订单[" + inTrx.getWo_id_fk() + "]中没有箱子["
					+ inTrx.getMtrl_box_id() + "]!";
			return rtnBeen.getlRc();
		} else {
			ret_prd_in = (Ret_prd_in) rtnBeen.getObj();
			String fab_id_shop = "";
			String fab_id_tool = "";
			String hql = "FROM Bis_data where data_cate = 'DEST' and data_id = '" + ret_prd_in.getStb_shop() + "'" ;	
			List<Bis_data> dataList = new ArrayList<Bis_data>();
			dataList = bisDataDao.find(hql);
			if(!dataList.isEmpty()){
				fab_id_shop = dataList.get(0).getData_item();
			}

			outTrx.setRet_prd_in(ret_prd_in);
		}
		String sql = "FROM Ret_prd_info_shtc WHERE box_id_fk in ('" + inTrx.getMtrl_box_id() +"', '"+ret_prd_in.getVdr_box_id()+"' )";
		prdInfoSthcList = retPrdInfoShtcDao.find(sql);
		if(!prdInfoSthcList.isEmpty()){
			outTrx.setOary1(prdInfoSthcList);
		}


		return _NORMAL;
	}
	/**
	 * Add prd_indt
	 * 
	 * @return RouteObj
	 */
	private long addPrdIndtForUnpack(Ret_prd_in ret_prd_in , String prd_seq_id, Integer i) {
		long lRc = 0;
		int slot_no_prdIn = ret_prd_in.getBox_std_qty();
		Ret_prd_indt ret_prd_indt = new Ret_prd_indt();
		Ret_prd_indtId ret_prd_indtId = new Ret_prd_indtId();
		String slot_no = inTrx.getIaryA().get(i).getSlot_no();

		ret_prd_indtId.setVdr_prd_seq_id(prd_seq_id);
		ret_prd_indtId.setAct_typ("R");
		ret_prd_indt.setId(ret_prd_indtId);

		ret_prd_indt.setPrd_stat(PROC_RELS);
		ret_prd_indt.setMtrl_box_id_fk(ret_prd_in.getId().getMtrl_box_id());
		if (Util.isSpaceCheck(slot_no)) {
			ret_prd_indt.setMtrl_slot_no(retPrdInDao.getNxSlotNoInMtrlBoxLoop(ret_prd_in));
		} else {
			int slot_no_inTx = Integer.valueOf(slot_no);
			if (retPrdIndtDao.hasUserInputSlotInMtrlBox(ret_prd_in,	slot_no)) {
				gszMsgBuf = "子格位[" + slot_no + "]在来料箱["
						+ ret_prd_in.getId().getMtrl_box_id() + "]中已经存在！";
				return E_XPRMA_SLOT_NO_EXIST;
			} else if (slot_no_inTx > slot_no_prdIn) {
				gszMsgBuf = "输入的子格位为[" + slot_no + "]，而来料箱["
						+ ret_prd_in.getId().getMtrl_box_id() + "]产品最大子格位为["
						+ ret_prd_in.getBox_std_qty() + "]！";
				return E_XPRMA_OVER_STD_CNT;
			} else {
				ret_prd_indt.setMtrl_slot_no(slot_no);
			}
		}
		ret_prd_indt.setWo_id_fk(inTrx.getWo_id_fk());
		ret_prd_indt.setEvt_timestamp(gEvtTimestamp);
		ret_prd_indt.setEvt_usr(inTrx.getEvt_usr());
		inTrx.getIaryA().get(i).setSlot_no(ret_prd_indt.getMtrl_slot_no()) ;

		retPrdIndtDao.saveOrUpdate(ret_prd_indt);

		return lRc;
	}
	/**
	 * Reset ret_prd_in
	 * 
	 * @param wo_id
	 * @param mtrl_box_id_fk
	 * @return {Ret_prd_in}
	 */
	private long resetRetPrdInForCancelUnpack(String wo_id,
			String mtrl_box_id_fk) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_prd_in ret_prd_in = new Ret_prd_in();

		rtnBeen = retPrdInDao.getRetPrdInByWoAndBox(wo_id, mtrl_box_id_fk);
		if (E_RET_PRD_IN + E_READ_NOT_FOUND == rtnBeen.getlRc()) {
			gszMsgBuf = new NotFoundRtnBeen("boxId:[" + mtrl_box_id_fk
					+ "], WoId:[" + wo_id + "] ", Ret_prd_in.class).toString();
			return rtnBeen.getlRc();
		} else {
			ret_prd_in = (Ret_prd_in) rtnBeen.getObj();
		}

		// Status
		if (PROC_RELS.equals(ret_prd_in.getPpbox_stat())) {
			if (ret_prd_in.getPrd_proc_qty().equals(1)) {
				ret_prd_in.setPpbox_stat(PROC_WFRL);
			}
		} else if (PROC_EMPTY.equals(ret_prd_in.getPpbox_stat())) {
			ret_prd_in.setPpbox_stat(PROC_RELS);
		} else {
			gszMsgBuf = "Mtrl box's status [" + ret_prd_in.getPpbox_stat()
					+ "] is invalied! Must [" + PROC_RELS + "] or ["
					+ PROC_RELS + "]";
			return E_XPUNPACK_MTRL_BOX_INVALIED;
		}
		ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty() - 1);
		ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty() + 1);
		ret_prd_in.setEvt_cate(EVT_CANCEL_UNPACK);
		ret_prd_in.setEvt_usr(inTrx.getEvt_usr());
		retPrdInDao.update(ret_prd_in);

		return _NORMAL;
	}	
	/**
	 * Delete ret_prd_indt
	 * 
	 * @param prd_seq_id
	 * @return
	 */
	private long delRetPrdIndtForCancelUnpack(String prd_seq_id) {
		Ret_prd_indtId ret_prd_indtId = new Ret_prd_indtId();

		ret_prd_indtId.setVdr_prd_seq_id(prd_seq_id);
		ret_prd_indtId.setAct_typ("R");

		retPrdIndtDao.deleteByKey(ret_prd_indtId);

		return _NORMAL;
	}
	
	/**
	 * 删除prd grd info
	 */
	private long delRetGrdInfoForCancelUnpack(String prd_seq_id) {
		Ret_prd_grd_info prd_grd_info = new Ret_prd_grd_info();
		prd_grd_info = retPrdGrdInfoDao.get(prd_seq_id);
		if(prd_grd_info != null){
			retPrdGrdInfoDao.delete(prd_grd_info);
		}
		return _NORMAL;
	}	
	
	private long checkLotInfo() {
		if (Util.isSpaceCheck(inTrx.getLot_id())) {
			return _NORMAL;
		}
		Ret_prd_lotId pk = new Ret_prd_lotId();
		pk.setLot_id(inTrx.getLot_id());
		pk.setLot_splt_id(LOT_SPLIT_0);
		Ret_prd_lot ret_prd_lot = retPrdLotDao.get(pk);
		if (ret_prd_lot == null) {
			return _NORMAL;
		} else {
			if (!ret_prd_lot.getWo_id_fk().equals(inTrx.getWo_id_fk())) {
				gszMsgBuf = "批次对应的内部订单[" + ret_prd_lot.getWo_id_fk()
						+ "]与箱子对应的内部订单[" + inTrx.getWo_id_fk() + "]不符,请检查！";
				return E_XPMOVEOUT_WO_UNMATCH;
			}
		}
		return _NORMAL;
	}
	/**
	 * Add lot info, just UPK use
	 * 
	 * @return
	 */
	private long addLotInfo(Integer prdLotCnt) {
		if (!Util.isSpaceCheck(inTrx.getLot_id())) {
			Ret_prd_lot ret_prd_lot = new Ret_prd_lot();
			Ret_prd_lotId ret_prd_lotId = new Ret_prd_lotId();
			ret_prd_lotId.setLot_id(inTrx.getLot_id());
			ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);

			ret_prd_lot = retPrdLotDao.get(ret_prd_lotId);
			if (null != ret_prd_lot) {
				ret_prd_lot.setLot_qty(prdLotCnt + ret_prd_lot.getLot_qty());

				ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
				ret_prd_lot.setEvt_time_d(gEvtTimeD);

				retPrdLotDao.update(ret_prd_lot);
			} else {
				ret_prd_lot = new Ret_prd_lot();
				ret_prd_lotId = new Ret_prd_lotId();
				ret_prd_lotId.setLot_id(inTrx.getLot_id());
				ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);
				ret_prd_lot.setId(ret_prd_lotId);
				ret_prd_lot.setLot_stat(PROC_WAIT);
				ret_prd_lot.setLot_qty(prdLotCnt);
				ret_prd_lot.setWo_id_fk(inTrx.getWo_id_fk());// 增加内部订单

				ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
				ret_prd_lot.setEvt_time_d(gEvtTimeD);

				ret_prd_lot.setStb_timestamp(gEvtTimestamp);

				retPrdLotDao.save(ret_prd_lot);
			}
		}

		return _NORMAL;
	}
}
